package com.cloud.controller;

import com.cloud.entity.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author .
 * created in 2020/1/10 14:42
 */

@Controller
public class UrlController {

    private Logger logger = LoggerFactory.getLogger(UrlController.class);

    @GetMapping(value = "/")
    public String toLogin() {
        return "login";
    }

    @GetMapping(value = "/provider")
    public String provider() {
        return "provider";
    }

    @GetMapping(value = "/type")
    public String type() {
        return "goods/type";
    }

    @GetMapping(value = "/goodInfo")
    public String goodInfo() {
        return "goods/info";
    }

    @GetMapping(value = "/warehouse")
    public String inGoods() {
        return "warehouse";
    }

    @GetMapping(value = "/order")
    public String order() {
        return "order";
    }

    @GetMapping(value = "/isLogin")
    @ResponseBody
    public Employee isLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (Employee) session.getAttribute("emp");
    }

    @GetMapping(value = "/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("emp");
        if (employee != null) {
            session.removeAttribute("user");
            session.invalidate();
            logger.info("员工【{}】已退出登录，即将跳转", employee.getName());
        }
        return "redirect:/";
    }

}
